#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018\9\6 0006 16:15
# @Author  : never mind
# @File    : utils_move_plot.py
import matplotlib.pyplot as plt
import constants as cons
import matplotlib.patches as mpatches
import select_path

def move_one_step(pair, ax):
    # 如何移动一步
    move_pingbancar(pair, ax=ax, reset_car=False)
    pass


def plot_car_park(ax):
    # 画出停车位
    num_right, num_up = cons.num[0], cons.num[1]
    delta_x, delta_y = cons.delta[0] * cons.alpha, cons.delta[1] * cons.alpha
    park_space_W, park_space_H = cons.park_space[0] * cons.alpha, cons.park_space[1] * cons.alpha

    for i in range(num_right)[0:]:
        base_park_y = delta_y + i * (park_space_H + delta_y)
        for j in range(num_up)[0:]:
            base_park_x = delta_x + j * (park_space_W + delta_x)
            rectangle = mpatches.Rectangle((base_park_x, base_park_y), park_space_W, park_space_H, color=cons.COLOR_PARK)
            ax.add_patch(rectangle)
        base_park_x = 1 * cons.alpha
    ax.grid()

def move_out_car(pair, ax):
    '''
    # 移出停放的车辆
    :param pair:
    :param ax:
    :param delta:
    :param park_space:
    :param alpha:
    :return:
    '''
    plot_per_car_park_occupied(pair, ax, color = cons.COLOR_PARK)
    print("moveout rectangle:x, y = %s, %s" % (pair[0] + 1, pair[1] + 1))

# move_out_car(pair_occupied[-1], ax)

def move_pingbancar(pair, ax, reset_car):
    '''

    :param pair:    [[1, 2], [2, 3], [3, 4], [4, 5]] 二维数组，每个元素代表一个位置
    :param ax:
    :param reset_car:
    :return:
    '''
    delta_x, delta_y = cons.delta[0]*cons.alpha, cons.delta[1]*cons.alpha
    park_W, park_H = cons.park_space[0]*cons.alpha, cons.park_space[1]*cons.alpha

    # 画出所有已经被占了的车位
    # plot_car_park_occupied(occupied, ax, delta, park_space, alpha)

    # 移动平板小车
    for i in range(len(pair)):
        plt.pause(cons.pause_seconds)
        # (i) 表示第几个车位的位置
        base_car_x = delta_x + (park_W + delta_x) * pair[i][0]
        base_car_y = delta_y + (park_H + delta_y) * pair[i][1]
        # 平板车的左下角坐标位置
        rectangle_x = base_car_x + 0.5 * (1 - cons.alpha_car) * park_W
        rectangle_y = base_car_y + 0.5 * (1 - cons.alpha_car) * park_H
        if not reset_car: # car-flatcar 一起移动
            try:
                car_rectangle.set_visible(False)  # 隐去前一个平板车位置
                # car_rectangle_car.set_visible(False)
                # ax.lines.remove(lines[0]) #是否显示轨迹线
                flatcar_rectangle.set_visible(False)
                pass
            except Exception:
                pass
            # 画出平板车
            car_rectangle = mpatches.Rectangle(
                (rectangle_x, rectangle_y),
                cons.alpha_car * park_W, cons.alpha_car * park_H, color= cons.COLOR_FLATCAR_CAR)
            car_rectangle_car = mpatches.Rectangle(
                (rectangle_x + 0.05*park_W, rectangle_y + 0.05*park_H),
                (cons.alpha_car-0.1) * park_W, (cons.alpha_car-0.1) * park_H, color=cons.COLOR_TRAJECTORY)
            ax.add_patch(car_rectangle)
            ax.add_patch(car_rectangle_car)
            # print("rectangle:x, y = %s, %s" % (rectangle_x, rectangle_y))
            # 画出平板车的轨迹线
            try:
                if (i == 0):
                    pass
                else:
                    # 线的坐标位置
                    f_x = delta_x + (park_W + delta_x) * pair[i - 1][0] + park_W * 0.5
                    f_y = delta_y + (park_H + delta_y) * pair[i - 1][1] + park_H * 0.5
                    t_x = base_car_x + park_W * 0.5
                    t_y = base_car_y + park_H * 0.5
                    # print("lines: from (%s, %s) to (%s, %s)" % (f_x, t_x, f_y, t_y))
                    lines = ax.plot([f_x, t_x], [f_y, t_y])
            except Exception:
                pass

            plt.pause(cons.pause_seconds)
        else: # 只移动平板车
            try:
                flatcar_rectangle.set_visible(False)
                pass
            except Exception:
                pass
            flatcar_rectangle = mpatches.Rectangle(
                (rectangle_x, rectangle_y),
                cons.alpha_car * park_W, cons.alpha_car * park_H, color=cons.COLOR_FLATCAR)
            ax.add_patch(flatcar_rectangle)
            # print("rectangle:x, y = %s, %s" % (rectangle_x, rectangle_y))

def plot_per_car_park_occupied(pair, ax, color = cons.COLORT_OCCUPIED):
    delta_x, delta_y = cons.delta[0] * cons.alpha, cons.delta[1] * cons.alpha
    park_W, park_H = cons.park_space[0] * cons.alpha, cons.park_space[1] * cons.alpha
    park_space_W, park_space_H = cons.park_space[0] * cons.alpha, cons.park_space[1] * cons.alpha

    # (i) 表示第几个车位的位置
    base_park_x = delta_x + (park_W + delta_x) * pair[0]
    base_park_y = delta_y + (park_H + delta_y) * pair[1]
    # 画出被占用的停车位
    rectangle = mpatches.Rectangle((base_park_x, base_park_y), park_space_W, park_space_H, color=color)
    ax.add_patch(rectangle)
    # if color == cons.COLORT_OCCUPIED:
    #     print("occupied rectangle:x, y = %s, %s" % (pair[0] + 1, pair[1] + 1))

def plot_car_park_occupied(pair_occupied, ax):
    '''
    画出被占有的车辆
    :param pair_occupied:
    :param ax:
    :param delta:
    :param park_space:
    :param alpha:
    :return:
    '''
    for pair in pair_occupied:
        plot_per_car_park_occupied(pair, ax)


def get_reset_position():
    return cons.reset_position

def reset_init_position(init_position, position):
    init_position[0] = position[0]
    init_position[1] = position[1]



def reset_flatcar(position, pair_occupied, ax):
    '''
    根据 position 得到停放平板小车的位置
    :return: 如果重置的位置和出入口相同，返回 false, 否则获取移动到重置位置的路径，返回其路径
    '''

    reset_position = get_reset_position()
    if reset_position != position:
        reset_pair = select_path.select_path(position, reset_position, pair_occupied, state='reset')
        if not reset_pair:
            pass
        reset_pair.append(reset_position)
        plot_car_park_occupied(pair_occupied, ax)
        move_pingbancar(reset_pair, ax, reset_car=True)

        reset_init_position(reset_position, reset_pair[-2])
        # return reset_pair
    else:
        #
        plot_per_car_park_occupied(position, ax, color= cons.COLOR_FLATCAR) #设置被移动之后的停车位为空位
        # return False